import { remoteDict } from "@/api/dicts";
import Vue from "vue";

class Dict {
    constructor(dict) {
        this.dict = dict;
    }

    async init(names) {
        const ps = [];
        names.forEach((name) => {
            Vue.set(this.dict, name, []);
            ps.push(
                remoteDict(name).then((data) => {
                    this.dict[name] = Object.freeze(data.content);
                })
            );
        });
        await Promise.all(ps);
    }
}


const install = function (Vue) {
    Vue.mixin({
        data() {
            if (
                this.$options.dicts instanceof Array &&
                this.$options.dicts.length > 0
            ) {
                return { dict: {} };
            } else {
                return {};
            }
        },
        created() {
            if (this.$options.dicts instanceof Array && this.$options.dicts.length > 0) {
                new Dict(this.dict).init(this.$options.dicts);
            }
        },
    });
};

export default { install };
